package com.itheima.health.service.impl;

import com.itheima.health.dao.RolePermissionMapper;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.RolePermission;
import com.itheima.health.service.RolePermissionService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class RolePermissionServiceImpl implements RolePermissionService {
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public void add(RolePermission rolePermission) {
        rolePermissionMapper.insert(rolePermission);
    }

    @Override
    public List<Integer> findpermissionIdsById(Integer id) {
        RolePermission rolePermission = new RolePermission();
        rolePermission.setRoleId(id);
        List<RolePermission> rolePermissions = rolePermissionMapper.select(rolePermission);
        List<Integer> integers = new ArrayList<>();
        for (RolePermission rolePermission1 : rolePermissions) {
            Integer permissionId = rolePermission1.getPermissionId();
            integers.add(permissionId);
        }
        return integers;
    }

    @Override
    public void edit(Role role, Integer[] permissionIds) {
        RolePermission rolePermission = new RolePermission();
        rolePermission.setRoleId(role.getId());
        rolePermissionMapper.delete(rolePermission);
        for (Integer permissionId : permissionIds) {
            RolePermission rolePermission1 = new RolePermission();
            rolePermission1.setRoleId(role.getId());
            rolePermission1.setPermissionId(permissionId);
            rolePermissionMapper.insert(rolePermission1);
        }
    }
}
